package com.exception;


import com.enums.ErrorCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.util.Set;

@ControllerAdvice
@Slf4j
public class ExceptionHandler {

    @org.springframework.web.bind.annotation.ExceptionHandler(RuntimeException.class)
@ResponseBody
    private ResponseEntity<Object> handlerExcaption(RuntimeException exception){
        log.error(exception.getMessage());
//        ConstraintViolationException exs = (ConstraintViolationException) exception;
//
//        Set<ConstraintViolation<?>> violations = exs.getConstraintViolations();
//        for (ConstraintViolation<?> item : violations) {
//                 /**打印验证不通过的信息*/
//            System.out.println(item.getPropertyPath());
//        }
        if(exception instanceof ValidateException) {//参数校验异常
            ValidateException validateException=(ValidateException)exception;
            return ResponseEntity.status(ErrorCodeEnum.PARAM_ERROR.getCode()).body(validateException.getMessageArray());
        }
        return ResponseEntity.status(ErrorCodeEnum.SYS_ERROR.getCode()).body("系统异常");
    }
}
